<?php

class edit_bans
{
	var $mess = '';
	
	var $data = Array();

	function edit_bans()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
		else if(isset($_POST['sendEmail']) && isset($_POST['id']))
		{
			$this->sendNotifEmail();
		}
		else if(isset($_POST['recalcPoints']) && isset($_POST['id']))
		{
			$this->recalculatePoints();
		}
		
		
		if( ! isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			//Setup Edit
			global $db, $lang;
			
			//get the ban
			$q = "SELECT b_id, b_user, b_startDate, b_endDate, b_license, b_comment, b_licensePoints, b_banCleared, pf_drivername, 
						 b_clearedPoints, date_format(b_emailSent, '%D %M %Y, %H:%i') as b_emailSent
				  FROM ". BANS_TABLE .  ", " . DRIVERNAME_TABLE . " 
				  WHERE user_id = b_user AND b_id = " . $_POST['itemID'];
			$result = $db->sql_query($q);
			if($row = $db->sql_fetchrow($result)) 
			{
				$this->data = $row;
				$this->data['b_comment'] = formatFormOutput($this->data['b_comment']);
				$this->data['driverOptions'] = '<option value="' . $row['b_user'] . '">'. $row['pf_drivername'] . "</option>";
				$this->data['clearedOptions'] = '<option value="0"' . ($row['b_banCleared'] == 0 ? 'SELECTED' : '') . ">" . $lang['No'] . "</option>";
				$this->data['clearedOptions'] .= '<option value="1"' . ($row['b_banCleared'] == 1 ? 'SELECTED' : '') . ">" . $lang['Yes'] . "</option>";
				
				$licenseOptions = '<option value="0" ' . ($row['b_license'] == 0 ? 'SELECTED' : '') . '>' . $lang['AllEvents'] . '</option>';
				
				//get the license
				$q = "SELECT l_id, l_name
					  FROM ". LICENSES_TABLE . "
					  ORDER BY l_order ASC";
				
				$result = $db->sql_query($q);
				while($l = $db->sql_fetchrow($result)) 
				{
					$licenseOptions .=  '<option value="' . $l['l_id'] . '" ' . 
											($row['b_license'] == $l['l_id'] ? 'SELECTED' : '') . 
											'>' . $l['l_name'] . '</option>';
				}
				
				$this->data['licenseOptions'] = $licenseOptions;
				
				
				$this->data['mess'] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$this->data['valid'] = true;
				
			}
			else
			{
				$this->data['mess'] = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
				$this->data['valid'] = false;
			}
		}
		else
		{
			//setup add
			global $db, $lang;
			
			$this->data['b_id'] = '';
			$this->data['b_user'] = '';
			$this->data['b_startDate'] = '';
			$this->data['b_endDate'] = '';
			$this->data['b_comment'] = '';
			$this->data['b_license'] = 0;
			$this->data['b_licensePoints'] = 0;
			$this->data['b_banCleared'] = 0;
			$this->data['b_clearedPoints'] = 0;
			$this->data['b_emailSent'] = '';
			$this->data['driverOptions'] = '';
				
			//get the ban
			$q = "SELECT d.user_id, pf_drivername FROM ". DRIVERNAME_TABLE . " d, " . USERSGROUPS_TABLE . " g
				  WHERE d.user_id = g.user_id AND g.group_id = 3
				  ORDER BY pf_drivername ASC";
			$result = $db->sql_query($q);
			while($row = $db->sql_fetchrow($result)) 
			{
				$this->data['driverOptions'] .= '<option value="' . $row['user_id'] . '">'. $row['pf_drivername'] . "</option>";
			}
			
				
			$licenseOptions = '<option value="0" SELECTED>' . $lang['AllEvents'] . '</option>';
				
			//get the license
			$q = "SELECT l_id, l_name
				  FROM ". LICENSES_TABLE . "
				  ORDER BY l_order ASC";
				
			$result = $db->sql_query($q);
			while($l = $db->sql_fetchrow($result)) 
			{
				$licenseOptions .=  '<option value="' . $l['l_id'] . '">' . $l['l_name'] . '</option>';
			}
				
			$this->data['licenseOptions'] = $licenseOptions;
				
			$this->data['clearedOptions'] = '<option value="0">' . $lang['No'] . "</option>";
			$this->data['clearedOptions'] .= '<option value="1">' . $lang['Yes'] . "</option>";
			
				
			$this->data['mess'] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		
				
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		if( ! isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	
	function recalculateExpiry()
	{
		global $db;
			
		$q = "UPDATE ". INCIDENTADMINREVIEW_TABLE ."
			  SET ar_licenseexpiry =
  					(SELECT DATE_ADD(DATE_ADD(ar_date, INTERVAL l_pointDuration MONTH), INTERVAL sum(DATEDIFF(b_endDate, b_startDate)) DAY)
   					 FROM ". EVENTS_TABLE.", ". CHAMPIONSHIPS_TABLE .", ". LICENSES_TABLE .", ". INCIDENTS_TABLE .", ". BANS_TABLE ."
   					 WHERE ar_incident = in_id AND in_event = e_id AND e_championship = c_id AND l_id = c_license AND 
					 	   b_user = ar_driver AND (b_license = 0 or b_license = l_id))
			  WHERE ar_admin = 0 AND ar_licensepoints > 0 AND ar_driver = " . $_POST['driver'];
			   
		$db->sql_query($q);
	}
	
	function sendNotifEmail()
	{
		global $db, $lang, $www_path;
		
		include_once('includes/email.php');
	
		$q = "SELECT b_user, b_user, date_format(b_startDate, '%D %M %Y') as b_startDateF, date_format(b_endDate, '%D %M %Y') as b_endDateF, b_license,
					 b_comment, b_licensePoints, b_banCleared, pf_drivername, b_clearedPoints, user_email, ul.l_name, bl.l_name as ban_license
				  FROM " . DRIVERNAME_TABLE . " d, " . USERS_TABLE . " u, " . LICENSES_TABLE . " ul, "  . LCUSERS_TABLE . ", " . BANS_TABLE . " 
				  LEFT JOIN " . LICENSES_TABLE . " bl ON bl.l_id = b_license
				  WHERE u.user_id = d.user_id AND u.user_id = b_user AND u_id = b_user AND u_license = ul.l_id AND b_id = " . $_POST['id'];
				 
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$email = new email('driver_ban', $row);
			
			if($row['b_licensePoints'] == 0)
				$email->addReplacement('{AUTOMATIC_INSERT}' , '');
			else
				$email->addReplacement('{AUTOMATIC_INSERT}' , $lang['EmailBannedAutomaticInsertion']);
				
			if($row['b_license'] == 0)
				$email->addReplacement('{EVENTS}' , $lang['AllEvents']);
			else
				$email->addReplacement('{EVENTS}' , $lang['EmailWarningBanNearAll'] . ' ' . $row['ban_license'] . ' ' . $lang['Events']);
			
			
			$email->addReplacement('{COMMENT}' , 'b_comment', 1);
			$email->addReplacement('{BAN_POINTS}' , 'b_licensePoints', 1);
			$email->addReplacement('{BAN_START}' , 'b_startDateF', 1);
			$email->addReplacement('{BAN_END}' , 'b_endDateF', 1);
			$email->addReplacement('{CURRENT_LICENSE}' , 'l_name', 1);
			$email->addReplacement('{EMAILADRESS}' , 'user_email', 1);
			$email->addReplacement('{DRIVERNAME}' , 'pf_drivername', 1);
			$email->addReplacement('{LICENSELINK}' , $www_path . 'drivers.php?mode=license&id=' . $row['b_user']);
									
			$email->sendEmail();
			
			$qUpd = "update  ". BANS_TABLE . " SET b_emailSent = NOW() WHERE b_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage($lang['EmailSentMessage'], $lang['EmailSentTitle']);
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select pf_drivername from ". BANS_TABLE . ", " . DRIVERNAME_TABLE . " Where user_id = b_user AND b_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". BANS_TABLE . " Where b_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['pf_drivername'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". BANS_TABLE . " (b_user, b_startDate, b_endDate, b_license, b_licensePoints, b_banCleared, b_comment, b_clearedPoints) values(" . 
							$_POST['driver'] . ", '" . $_POST['startDate'] . "', '" . $_POST['endDate'] . "', " . $_POST['license'] .", " . 
							$_POST['licensePoints'] .", " . $_POST['cleared'] . ", '" . $_POST['comment'] . "', " . $_POST['clearedPoints'] . ")" ;
			$db->sql_query($qUpd);
			$this->recalculateExpiry();
			$this->mess = new addMessage(str_replace('{entry}', 'ban', $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". BANS_TABLE . " SET b_startDate = '" . $_POST['startDate'] .
											     "', b_endDate = '" . $_POST['endDate'] .
												 "', b_comment = '" . $_POST['comment'] .
												 "', b_license = " . $_POST['license'] .
												  ", b_licensePoints = " . $_POST['licensePoints'] .
												  ", b_banCleared = " . $_POST['cleared'] .
												  ", b_clearedPoints = " . $_POST['clearedPoints'] .
											   " WHERE b_id = " . $_POST['id'] ;
			$db->sql_query($qUpd);
			$this->recalculateExpiry();
			$this->mess = new addMessage(str_replace('{entry}', 'ban', $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		
		$valid = $valid && is_numeric( $_POST['license']) && is_numeric( $_POST['licensePoints']) && is_numeric( $_POST['cleared']);
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $lang;
		
		$this->data['mess']->render();
		
		if($this->data['valid'])
		{
			$data = $this->data;
			$addS = '';
			$addE = '';
					
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
					
			include $skin_dir . 'admin/editban.tpl';
		}
	}

	function renderAdd()
	{
		global $skin_dir, $lang;
		
		$this->data['mess']->render();
		$data = $this->data;
					
		$addS = '<!--';
		$addE = '-->';
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
					
		include $skin_dir . 'admin/editban.tpl';
	}
	
}
?>